package com.beep.erp.model.purchase;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beep.erp.R;
import com.beep.erp.common.bean.PurchaseListBean;

import java.text.FieldPosition;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者 by Jolly on 2018/8/25.
 */

public class PurchaseListAdapter extends RecyclerView.Adapter<PurchaseListAdapter.PurchaseViewHolder> {


    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private OnItemClickLitener mOnItemClickLitener;
    private ArrayList<PurchaseListBean.BizBuyingRequisitionListBean> mPurchaseListBean;
    private String goodStatus;

    public PurchaseListAdapter(Context context, ArrayList<PurchaseListBean.BizBuyingRequisitionListBean> mPurchaseListBean) {
        mContext = context;
        this.mPurchaseListBean = mPurchaseListBean;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener) {
        mOnItemClickLitener = onItemClickLitener;
    }

    @Override
    public PurchaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PurchaseViewHolder(mLayoutInflater.inflate(R.layout.item_purchase_list, parent, false));
    }

    @Override
    public void onBindViewHolder(PurchaseViewHolder holder, int position) {
        holder.tvGoodsNum.setText(mPurchaseListBean.get(position).getPurchaseCode());
        String status = mPurchaseListBean.get(position).getStatus();
        goodStatus = "";
        switch (status) {
            case "1":
                goodStatus = mContext.getString(R.string.wait_check_goods);
                holder.btnInto.setBackground(mContext.getResources().getDrawable(R.drawable.btn_bg_red));
                break;
            case "2"://2,4,12入库按钮不可点击
                goodStatus = mContext.getString(R.string.waiting_for_settlement);
                holder.btnInto.setBackground(mContext.getResources().getDrawable(R.drawable.btn_bg_gray));
                break;
            case "4":
                goodStatus = mContext.getString(R.string.scrap_list);
                holder.btnInto.setClickable(false);
                holder.btnInto.setBackground(mContext.getResources().getDrawable(R.drawable.btn_bg_gray));
                break;
            case "11":
                goodStatus = mContext.getString(R.string.part);
                holder.btnInto.setBackground(mContext.getResources().getDrawable(R.drawable.btn_bg_red));
                break;
            case "12":
                goodStatus = mContext.getString(R.string.all);
                holder.btnInto.setBackground(mContext.getResources().getDrawable(R.drawable.btn_bg_gray));
                break;
        }
        holder.tvGoodsStatus.setText(goodStatus);
        holder.tvGoodsDisc.setText(mPurchaseListBean.get(position).getDetailGoodsName());
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(v -> {
                mOnItemClickLitener.onItemClick(v, position);
            });
            holder.btnInto.setOnClickListener(view -> {
                mOnItemClickLitener.onIntoClick(view, position);
            });
        }
    }

    @Override
    public int getItemCount() {
        return mPurchaseListBean.size();
    }

    public class PurchaseViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;
        @BindView(R.id.tv_goods_status)
        TextView tvGoodsStatus;
        @BindView(R.id.tv_goods_disc)
        TextView tvGoodsDisc;
        @BindView(R.id.rl_goods_msg)
        RelativeLayout rlGoodsMsg;
        @BindView(R.id.btn_into)
        TextView btnInto;

        public PurchaseViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onIntoClick(View view, int position);
    }
}
